layui.use('table', function () {
    var table = layui.table;
    table.render({
        elem: '#demo',
        url: '/info/courseInfo/list',
        toolbar: '#toolbar',
        page: true,
        cols: [[
            {type: 'checkbox'},
            {type: 'numbers',title: '序号'},
            {field: 'cnum',title: '课程编号'},
            {field: 'cname',title: '课程名称'},
            {field: 'ctime', title: '课时'},
            {field: 'operation',title: '操作',toolbar: '#rowTools',width:120}
        ]],
        height: 'full-20'
    });
    var urls;
    // 行工具栏监听
    table.on('tool(test)', function (obj) {
        switch (obj.event) {
            case 'edit':
                urls = '/info/courseInfo/update'
                layer.open({
                    type: 1,
                    title: $(this).text(),
                    content: $('.form'),
                    cancel: function(){ 
                        //右上角关闭回调
                        $('.cnum').val('');
                        $('.cname').val('');
                        $('.ctime').val('');
                    }
                });
                $('.cnum').val(obj.data.cnum);
                $('.cname').val(obj.data.cname);
                $('.ctime').val(obj.data.ctime);
                break;
            case 'del':
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/info/courseInfo/delete',
                        data: {cnum: obj.data.cnum},
                        success: function (params) {
                            if (params.affectedRows > 0) {
                                layer.msg('成功删除' + params.affectedRows +'数据!');
                                obj.del();
                            } else {
                                layer.msg('删除失败!');
                            }
                            layer.close(index);
                        }
                    });                   
                });
                break;
        }     
    });
    // 头部工具栏监听
    table.on('toolbar(test)', function (obj) {   
        switch (obj.event) {
            case 'search':
                var inputVal = $('.layui-input').val();
                var temp = inputVal;
                if ($.trim(inputVal) == '' || $.trim(inputVal) == null) {
                    return layer.msg('输入不能为空!');
                }
                table.reload('demo', {
                    url: '/info/courseInfo/search',
                    page: {curr: 1},
                    where: {cnum: inputVal}
                });
                $('.layui-input').val(temp);
                break;
            case 'refresh':
                reloads();
                break;
            case 'dels':
                var checkStatus = table.checkStatus('demo');
                var len = checkStatus.data.length;
                if (len <= 0) {
                    return layer.msg('请先选择要删除的数据!');
                }
                var strNums = [];
                for (var i = 0; i < len; i++) {
                    strNums.push(checkStatus.data[i].cnum);
                }
                var cnums = strNums.join(',');
                layer.confirm('真的删除吗?', function () {
                    $.ajax({
                        url: '/info/courseInfo/delete',
                        data: {cnum: cnums},
                        success: function(params) {
                            if (params.affectedRows > 0) {
                                layer.msg('成功删除' + params.affectedRows + '数据!')
                                reloads();
                            } else {
                                layer.msg('删除失败!')
                            }
                        }
                    });
                });
                break;
            case 'add':
                urls = '/info/courseInfo/add';
                layer.open({
                    title: $(this).text(),
                    type: 1,
                    content: $('.form')
                });
                break;
        }
    });
    $('.submit').click(function() {
        var list = {
            cnum: $.trim($('.cnum').val()),
            cname: $.trim($('.cname').val()),
            ctime: $.trim($('.ctime').val())
        };
        $.ajax({
            url: urls,
            type: 'POST',
            data: list,
            success: function (params) {
                if (params.affectedRows > 0) {
                    layer.msg('操作成功！');
                    reloads();
                } else if (params.error){
                    layer.msg(params.msg);
                } else {
                    layer.msg('修改失败！');
                }
            }
        });
    });
    function reloads(){
        table.reload('demo', {
            url: '/info/courseInfo/list'
        });
    }
});